package io.renren.modules.draw_money_log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.CommonUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.draw_money_log.dao.V9DrawMoneyLogDao;
import io.renren.modules.draw_money_log.entity.V9DrawMoneyLogEntity;
import io.renren.modules.draw_money_log.entity.params.V9drawMoneyLogParams;
import io.renren.modules.draw_money_log.service.V9DrawMoneyLogService;
import io.renren.modules.withdraw_cash.service.impl.V9WithdrawCashServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("v9DrawMoneyLogService")
public class V9DrawMoneyLogServiceImpl extends ServiceImpl<V9DrawMoneyLogDao, V9DrawMoneyLogEntity> implements V9DrawMoneyLogService {


    private static Logger logger = LoggerFactory.getLogger(V9WithdrawCashServiceImpl.class);

    @Autowired
    V9DrawMoneyLogDao V9DrawMoneyLogDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String dml_ordersn = (String)params.get("dml_ordersn");
        String special_id = (String)params.get("special_id");
        String dml_status = (String)params.get("dml_status");
        String start = (String)params.get("start");
        String end = (String)params.get("end");
        Integer sInteger=0,eInteger=0;
        Integer status=null;
        if (StringUtils.isNotBlank(start)) {
            sInteger= CommonUtils.timeStrToTimeStamp3(start);
            eInteger=CommonUtils.timeStrToTimeStamp3(end);
        }
        if (StringUtils.isNotBlank(dml_status)) {
            status=Integer.parseInt(dml_status);
        }

        IPage<V9DrawMoneyLogEntity> page = this.page(
                new Query<V9DrawMoneyLogEntity>().getPage(params,"dml_id",false),
                new QueryWrapper<V9DrawMoneyLogEntity>()
                        .like(StringUtils.isNotBlank(dml_ordersn), "dml_ordersn", "%"+dml_ordersn+"%")
                        .like(StringUtils.isNotBlank(special_id), "special_id", "%"+special_id+"%")
                        .eq(status!=null, "dml_status", dml_status)
                        .eq(status!=null, "dml_status", dml_status)
                        .between(StringUtils.isNotBlank(start), "dml_addtime", sInteger, eInteger));
        return new PageUtils(page);
    }

    @Override
    public R pass(V9drawMoneyLogParams params) {
        V9DrawMoneyLogEntity byId = this.getById(params.getId());
        if (byId.getDmlStatus() == 2 || byId.getDmlStatus() == 3){
            return R.error("此申请已通过或退回,不能再次操作");
        }
        if (params.getType() == 1){
            V9DrawMoneyLogDao.pass(params.getId());
        }
        if (params.getType() == 2){
            V9DrawMoneyLogDao.returned(params.getId());
        }
        return R.ok();
    }
    }

